package com.example.schoolblog.blog.Controller;


import com.example.schoolblog.blog.Cache.TagCache;
import com.example.schoolblog.blog.Dto.QuestionDTO;
import com.example.schoolblog.blog.Model.Question;
import com.example.schoolblog.blog.Model.User;
import com.example.schoolblog.blog.Service.QuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;


@Controller
public class PublicController {

    @Autowired
    private QuestionService questionService;

    //编辑文章
    @GetMapping("/public/{id}")
    public String edit(@PathVariable(name="id") Long id,
                       Model model){
        //根据id获取问题
        QuestionDTO question = questionService.getById(id);
        //将问题的内容封装
        model.addAttribute("title",question.getTitle());
        model.addAttribute("description",question.getDescription());
        model.addAttribute("tag",question.getTag());
        model.addAttribute("id",question.getId());
        model.addAttribute("tags", TagCache.get());
        //返回问题页面供用户编辑
        return "public";
    }


    //直接点击发布时跳转的接口
    @GetMapping("/public")
    public String getPublic(Model model){
        model.addAttribute("tags", TagCache.get());
        return "public";
    }

    //创建或者编辑问题
    @PostMapping("/public")
    public String doPublic(@RequestParam("title") String title,
                           @RequestParam("description") String description,
                           @RequestParam("tag") String tag,
                           HttpServletRequest request,
                           Model model,
                           @RequestParam(name="id", required = false) Long id){

        //将文章封装进model
        model.addAttribute("title",title);
        model.addAttribute("description",description);
        model.addAttribute("tag",tag);
        model.addAttribute("tags", TagCache.get());

        //判断是否有空，有则提示不能为空
        if(title == null || title == ""){
            model.addAttribute("error","标题不能为空");
            return "public";
        }
        if(description == null || description == ""){
            model.addAttribute("error","问题补充不能为空");
            return "public";
        }
        if(tag == null || tag == ""){
            model.addAttribute("error","标签不能为空");
            return "public";
        }


        //判断用户是否输入非法标签
        String invalid = TagCache.filterInvalid(tag);
        if (StringUtils.isNotBlank(invalid)) {
            model.addAttribute("error", "输入非法标签:" + invalid);
            return "public";
        }
        //判断用户是否登录，未登录不允许发表
        User user = (User) request.getSession().getAttribute("user");
        if(user == null){
            model.addAttribute("error","用户未登录");
            return "public";
        }


        //封装文章的内容
        Question question = new Question();
        question.setTitle(title);
        question.setDescription(description);
        question.setTag(tag);
        question.setCreator(user.getId());
        question.setGmtCreate(System.currentTimeMillis());
        question.setGmtModified(question.getGmtCreate());
        question.setId(id);
        //更新数据库
        questionService.createOrUpdate(question);
        return "redirect:/";
    }
}
